//
//  YouhuiViewController.m
//  PlayerWorld
//
//  Created by 汪的一声就哭了！ on 16/8/15.
//  Copyright © 2016年 刘成. All rights reserved.
//

#import "YouhuiViewController.h"
#import "YouhuiTableViewCell.h"
#import "YouhuiDetailViewController.h"
#import "AFNetworking.h"
#import "YouhuiModel.h"
#import "UIImageView+WebCache.h"
@interface YouhuiViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation YouhuiViewController
{
    NSMutableArray *_listArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self obtainData];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
    self.title = @"优惠活动";
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"YouhuiTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YouhuiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    YouhuiModel *model = [_listArr objectAtIndex:indexPath.row];
    NSLog(@"%@",model.A_L_Picture);
    [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://static.public.ms/lf/Template/Skin_1/images/activity/%@",model.A_L_Picture]]];
    cell.nameLabel.text = model.A_L_Title;
    cell.detailLabel.text = model.A_L_Info;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 244;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YouhuiModel *model = [_listArr objectAtIndex:indexPath.row];
    YouhuiDetailViewController *viewC = [[YouhuiDetailViewController alloc] init];
    viewC.name = model.A_L_Title;
    viewC.a_id = model.A_L_Id;
    [self.navigationController pushViewController:viewC animated:YES];
}
-(void)obtainData
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";
    
    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"Events" forKey:@"Model"];
    [parametersDic setObject:@"get_activity_list" forKey:@"Action"];
    [parametersDic setObject:@"0" forKey:@"index"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        //        NSLog(@"123%@",[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil]);
        NSArray *arr = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        _listArr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            YouhuiModel *model = [[YouhuiModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_listArr addObject:model];
        }
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
